package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.domain.vo.JtWarehouseVo;
import com.ruoyi.business.service.IJtWarehouseService;
import com.ruoyi.business.utils.SnGenerateUtil;
import com.ruoyi.business.vo.StocktakingVo;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.service.IJtStocktakingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 库存盘点Controller
 * 
 * @author Alan Zhang
 * @date 2025-06-13
 */
@Controller
@RequestMapping("/business/stocktaking")
public class JtStocktakingController extends BaseController
{
    private String prefix = "business/stocktaking";

    @Autowired
    private IJtStocktakingService jtStocktakingService;
    @Autowired
    private IJtWarehouseService jtWarehouseService;

    @RequiresPermissions("business:stocktaking:view")
    @GetMapping()
    public String stocktaking(ModelMap mmap)
    {
        List<JtWarehouseVo> warehouseVoList = jtWarehouseService.selectJtWarehouseByStatus(0);
        mmap.put("warehouseVoList", warehouseVoList);
        return prefix + "/stocktaking";
    }

    /**
     * 查询库存盘点列表
     */
    @RequiresPermissions("business:stocktaking:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtStocktaking jtStocktaking)
    {
        startPage();
        List<JtStocktaking> list = jtStocktakingService.selectJtStocktakingList(jtStocktaking);
        return getDataTable(list);
    }

    /**
     * 查看订单
     */
    @RequiresPermissions(value = {"business:order:view","distributor:order:view"},logical = Logical.OR)
    @GetMapping("/view/{stocktakingId}")
    public String view(@PathVariable("stocktakingId") Long stocktakingId, ModelMap mmap)
    {
        JtStocktaking jtStocktaking = jtStocktakingService.selectJtStocktakingByStocktakingId(stocktakingId);
        if(jtStocktaking.getWarehouseId() != null){
            JtWarehouse jtWarehouse = jtWarehouseService.selectJtWarehouseByWarehouseId(jtStocktaking.getWarehouseId());
            jtStocktaking.setWarehouseName(jtWarehouse.getWarehouseName());
        }

        mmap.put("jtStocktaking", jtStocktaking);
        return prefix + "/view";
    }

    /**
     * 导出库存盘点列表
     */
    @RequiresPermissions("business:stocktaking:export")
    @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtStocktaking jtStocktaking)
    {
        List<JtStocktaking> list = jtStocktakingService.selectJtStocktakingList(jtStocktaking);
        ExcelUtil<JtStocktaking> util = new ExcelUtil<JtStocktaking>(JtStocktaking.class);
        return util.exportExcel(list, "库存盘点数据");
    }

    /**
     * 新增库存盘点
     */
    @RequiresPermissions("business:stocktaking:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("inventoryListNumber", SnGenerateUtil.stocktakingOrderNumber());
        return prefix + "/add";
    }

    /**
     * 新增保存库存盘点
     */
    @RequiresPermissions("business:stocktaking:add")
    @Log(title = "库存盘点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JtStocktaking jtStocktaking)
    {
        return toAjax(jtStocktakingService.insertJtStocktaking(jtStocktaking));
    }

    /**
     * 修改库存盘点
     */
    @RequiresPermissions("business:stocktaking:edit")
    @GetMapping("/edit/{stocktakingId}")
    public String edit(@PathVariable("stocktakingId") Long stocktakingId, ModelMap mmap)
    {
        JtStocktaking jtStocktaking = jtStocktakingService.selectJtStocktakingByStocktakingId(stocktakingId);
        mmap.put("jtStocktaking", jtStocktaking);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存盘点
     */
    @RequiresPermissions("business:stocktaking:edit")
    @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtStocktaking jtStocktaking)
    {
        return toAjax(jtStocktakingService.updateJtStocktaking(jtStocktaking));
    }

    /**
     * 删除库存盘点
     */
    @RequiresPermissions("business:stocktaking:remove")
    @Log(title = "库存盘点", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtStocktakingService.deleteJtStocktakingByStocktakingIds(ids));
    }

    /**
     * 确认生效
     */
    @RequiresPermissions("business:stocktaking:edit")
    @Log(title = "库存盘点-确认生效", businessType = BusinessType.DELETE)
    @PostMapping( "/validation")
    @ResponseBody
    public AjaxResult validation(String[] ids){
        if(StringUtils.isEmpty(ids)){
            return AjaxResult.error("请选择确认盘点的记录");
        }
        for(String stocktakingId : ids){
            JtStocktaking jtStocktaking = jtStocktakingService.selectJtStocktakingByStocktakingId(Long.valueOf(stocktakingId));
            if(jtStocktaking != null){

            }
        }
        return success();
    }


    /**
     * 模板 商品信息
     * @return
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<StocktakingVo> util = new ExcelUtil<>(StocktakingVo.class);
        return util.importTemplateExcel( "库存盘点导入");
    }


    @Log(title = "库存盘点-导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("business:stocktaking:edit")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Long warehouseId) throws Exception
    {
        ExcelUtil<StocktakingVo> util = new ExcelUtil<>(StocktakingVo.class);
        List<StocktakingVo> list = util.importExcel(file.getInputStream());
        String optName = ShiroUtils.getSysUser().getUserName();
        String message = jtStocktakingService.importStocktaking(list, optName, warehouseId);
        return AjaxResult.success(message);
    }
}
